package algorithms.que13;


import edu.princeton.cs.algs4.In;
import org.apache.el.lang.ELArithmetic;

import java.text.StringCharacterIterator;

/**
 * @功能
 * @创建时间 2020/7/8
 */
public class DelNodeEnd {

    public static void main(String[] args) throws CloneNotSupportedException {

        Node first = new Node();
        first.setItem("1");
        first.setNext(null);
        first.add("2");
        first.add("3");
//        while (first.getNext().hasNext()){
//            first=first.getNext();
//        }
//        first.setNext(null);
        System.out.println(first.delete(1).getNext().hasNext());
    //    System.out.println(find("3",first));
    }
    // 查询链表是否含有元素
    public static boolean find(String key,Node b){
        Node a = new Node();
        a.setNext(b.getNext());
        a.setItem(b.getItem());
        while (a.hasNext()&&a.getItem()!=key){
            a=a.getNext();
        }
        if(a.getItem()!=key){
            return false;
        }
        return true;
    }
    //删除节点后续节点
    public static void removeAfter(Node a){
        if (a.getNext()!=null&a.getNext().getNext()!=null){
            a.setNext(a.getNext().getNext());
        }
    }
    //插入后续节点
    public static void insertAfter(Node a,Node b){
        if(a.getNext()!=null&&b.getNext()!=null){
            a.setNext(b);
        }
    }
    //删除item为key的所有节点
    public static void remove(Node first,String key){
        Node pre = new Node();
        while (first.hasNext()){
            if(first.getItem().equals(key))
                first.del();
            else
            pre.add(first.getItem());
        }
        if(first.getItem().equals(key))
            first.del();
        while (pre.hasNext()){
            first.add(pre.getItem());
            pre.del();
        }
        first.add(pre.getItem());
    }
    //返回键值最大的值
    public static Integer max(Node cur){
        Integer tmp;
        if(cur!=null)
        tmp= Integer.valueOf(cur.getItem());
        else
            tmp=0;
        while (cur.hasNext()) {
            cur = cur.getNext();
            if(tmp<Integer.valueOf(cur.getItem()))
                tmp=Integer.valueOf(cur.getItem());
        }
        return tmp;
    }
    //递归找最大
    public static Integer getMax(Node cur){
        if(cur.hasNext()){
            if(Integer.valueOf(cur.getItem())<=Integer.valueOf(cur.getNext().getItem())){
                cur.del();
            }else if(Integer.valueOf(cur.getItem())>Integer.valueOf(cur.getNext().getItem())){
                cur.setNext(cur.getNext());
            }
            getMax(cur);
        }
        return Integer.valueOf(cur.getItem());
    }


}

class Node{

    private String item;
    private Node next;

    public void add(String item)// throws CloneNotSupportedException
    {
        if(this.next!=null) {
        Node oldfirst = new Node();
        oldfirst.setItem(this.item);
        oldfirst.setNext(this.next);
        //  oldfirst= (Node) this.clone();

        this.setItem(item);
        this.setNext(oldfirst);}
        else {
            this.setItem(item);
            this.setNext(null);
    }

    }
    //删除链表第p个元素
    public  Node delete(Integer p){
        Node pre = new Node();

        for (int i =1;i<p;i++){
            pre.add(this.item);
            this.setItem(next.item);
            this.setNext(next.next);

        }
       this.del();
        while (pre.hasNext()){
            this.add(pre.item);
            pre.setNext(pre.next.next);
            pre.setItem(pre.next.item);
        }
        return this;
    }

    public boolean hasNext(){
        return !(this.next==null);
    }
    public Node del(){
        this.setItem(next.item);
        this.setNext(next.next);
        return this;
    }
    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }
}